<template>
  <div>
    <div v-for="(item, index) in list" :key="index">
      <div>{{ item }}</div>
    </div>
    <div class="block">
      <span class="demonstration">完整功能</span>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="100"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
export default {
  mounted() {
    // 进来默认渲染10条
    this.list = this.listDate.slice(0, this.pageSize);
  },
  methods: {
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val; //改变每页数量时更新到data数据中
      this.handleCurrentChange(this.currentPage); //改变每页数量时调用更新渲染长度
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val; //改变页码时更新到data数据中

      this.list = this.listDate.slice( //改变页码时切割不同段数据
        val * this.pageSize - this.pageSize,
        val * this.pageSize
      );
    }
  },
  data() {
    return {
      listDate: [
        1,
        2,
        3,
        4,
        5,
        6,
        7,
        8,
        9,
        10,
        11,
        12,
        13,
        14,
        15,
        16,
        17,
        18,
        19,
        20,
        21,
        22,
        23,
        24,
        25,
        26,
        27,
        28,
        29,
        30,
        31,
        32,
        33,
        34,
        35,
        36,
        37,
        38,
        39,
        40
      ],
      list: [],
      pageSize: 10,
      currentPage: 1
    };
  }
};
</script>
<style lang="less" scoped>
.box {
  position: relative;

  .el-form {
    width: 700px;
    position: absolute;
    left: 0;
    right: 0;
    margin: 100px auto;
  }
}
</style>
